package com.qian.fmall.cart.service.impl;

import com.qian.fmall.cart.service.CartInterface;
import com.qian.fmall.commons.ResultSet;
import com.qian.fmall.commons.vo.ShoppingCartVo;
import com.qian.fmall.mapper.CartMapper;
import com.qian.fmall.mapper.ProductMapper;
import com.qian.fmall.mapper.ProductSkuMapper;
import com.qian.fmall.pojo.Product;
import com.qian.fmall.pojo.ProductSku;
import com.qian.fmall.pojo.ShoppingCart;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
public class CartService implements CartInterface {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Override
    public ResultSet addShoppingCart(ShoppingCart shoppingCart) {
        int row = cartMapper.addToShoppingCart(shoppingCart);
        if(row>0)
        {
            return ResultSet.ok("success");
        }
        return ResultSet.error("error");
    }

    @Override
    public ResultSet showShoppingCart(String userId) {
        ArrayList<ShoppingCartVo> list = new ArrayList<ShoppingCartVo>();
        List<ShoppingCart> shoppingCartByUserId = cartMapper.getShoppingCartByUserId(userId);
        for (ShoppingCart shoppingCart : shoppingCartByUserId) {
            ShoppingCartVo vo = new ShoppingCartVo();
            Product product = productMapper.getProductByProductId(shoppingCart.getProductId());
            vo.setProduct(product);
            ProductSku skuBySkuId = productSkuMapper.getProductSkuBySkuId(shoppingCart.getSkuId());
            vo.setProductSku(skuBySkuId);
            vo.setCart(cartMapper.getShoppingCartByCartId(shoppingCart.getCartId()));
            list.add(vo);
        }
        return ResultSet.ok("success",list);
    }

    @Override
    public ResultSet getShoppingCartSByCartIds(String cartIds) {
        ArrayList<ShoppingCartVo> carts = new ArrayList<>();
        if(cartIds.contains(","))
        {
            String[] split = cartIds.split(",");
            for (String s : split) {
                ShoppingCartVo vo = new ShoppingCartVo();
                ShoppingCart cart = cartMapper.getShoppingCartByCartId(Integer.parseInt(s));
                vo.setCart(cart);
                Product product = productMapper.getProductByProductId(cart.getProductId());
                vo.setProduct(product);
                ProductSku productSku = productSkuMapper.getProductSkuBySkuId(cart.getSkuId());
                vo.setProductSku(productSku);
                carts.add(vo);
            }
        }
        else
        {
            ShoppingCartVo vo = new ShoppingCartVo();
            ShoppingCart cart = cartMapper.getShoppingCartByCartId(Integer.parseInt(cartIds));
            vo.setCart(cart);
            Product product = productMapper.getProductByProductId(cart.getProductId());
            vo.setProduct(product);
            ProductSku productSku = productSkuMapper.getProductSkuBySkuId(cart.getSkuId());
            vo.setProductSku(productSku);
            carts.add(vo);
        }
        return ResultSet.ok("success",carts);
    }
}
